package com.asm.controller.api;

import com.asm.dto.PageResult;
import com.asm.dto.Result;
import com.asm.service.DistrictService;
import com.asm.vo.sys.DistrictVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * @author tanzujie
 */
@RestController
public class DistrictController {
    @Autowired
    private DistrictService districtService;

    /**
     * 分页查询所有省市区
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/districts")
    public ResponseEntity<PageResult<DistrictVo>> getAllDistricts(Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", districtService.findAllDistricts(pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据Id获取省市区
     *
     * @param id
     * @return
     */
    @GetMapping("/district/{id}")
    public ResponseEntity<Result<DistrictVo>> getDistrictById(@PathVariable("id") Long id) {
        return new ResponseEntity(new Result().ok("success", districtService.findDistrictById(id)), HttpStatus.OK);
    }

    /**
     * 根据省名字类型分页查询省市区
     *
     * @param province
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/districts/province/{province}")
    public ResponseEntity<PageResult<DistrictVo>> getDistrictsByType(@PathVariable("province") String province, Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", districtService.findDistrictByProvince(province, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据城市名字分页查询省市区
     *
     * @param city
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/districts/city/{city}")
    public ResponseEntity<PageResult<DistrictVo>> getfindDistrictByCity(@PathVariable("city") String city, Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", districtService.findDistrictByCity(city, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 增加省市区
     *
     * @param districtVo
     * @return
     */
    @PostMapping("/district/add")
    public ResponseEntity<Result> addDistrict(@RequestBody DistrictVo districtVo) {
        districtService.addDistrict(districtVo);
        return new ResponseEntity(new Result().ok("add success"), HttpStatus.OK);
    }

    /**
     * 更新省市区
     *
     * @param districtVo
     * @return
     */
    @PutMapping("/district/update")
    public ResponseEntity<Result> updateDistrict(@RequestBody DistrictVo districtVo) {
        districtService.updateDistrict(districtVo);
        return new ResponseEntity(new Result().ok("update success"), HttpStatus.OK);
    }

    /**
     * 根据Id删除省市区
     *
     * @param id
     * @return
     */
    @DeleteMapping("/district/delete/{id}")
    public ResponseEntity<Result> deleteDistrict(@PathVariable("id") Long id) {
        districtService.deleteDistrict(id);
        return new ResponseEntity(new Result().ok("delete success"), HttpStatus.OK);
    }
}
